#!/bin/bash
#
# Copyright (c) 2014, Joyent, Inc. All rights reserved.
#
# Run the fwadm integration tests.
# Run `./runtests -h` for usage info.
#

if [ "$TRACE" != "" ]; then
    export PS4='${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
    set -o xtrace
fi
set -o errexit
set -o pipefail



#---- guard

if [[ ! -f "/lib/sdc/.sdc-test-no-production-data" ]]; then
    cat <<EOF
To run this test you must create the file:

    /lib/sdc/.sdc-test-no-production-data

after ensuring you have no production data on this SDC.
EOF
    exit 2
fi



#---- config

NAME=fw
TOP=$(cd $(dirname $0)/../; pwd)



#---- support functions

function fatal
{
    echo "$(basename $0): fatal error: $*"
    exit 1
}

function usage
{
    echo "Usage:"
    echo "  runtests [OPTIONS...]"
    echo ""
    echo "Options:"
    echo "  -f FILTER   Filter pattern (substring match) for test files to run."
}



#---- mainline

start_time=$(date +%s)

# Options.
opt_test_pattern=
while getopts "hf:" opt
do
    case "$opt" in
        h)
            usage
            exit 0
            ;;
        f)
            opt_test_pattern=$OPTARG
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

OUTPUT_DIR=/var/tmp/${NAME}test
echo "# Create output dir ($OUTPUT_DIR)."
rm -rf $OUTPUT_DIR
mkdir -p $OUTPUT_DIR



#---- start tests

FINAL_EXIT_CODE=0

for test in $(ls $TOP/test/integration | grep "\.test.js$"); do
    test_name=$(basename $test)
    set +o errexit
    set -o pipefail
    /usr/fw/test/runtest $TOP/test/integration/${test} | tee ${OUTPUT_DIR}/${test_name}.output.$$.tap
    TEST_EXIT_CODE=$?
    set +o pipefail
    set -o errexit
    if [[ ${TEST_EXIT_CODE} != 0 ]]; then
        test_errors=$((${test_errors} + 1))
        failed_tests=$(printf "${pwd}/test/integration/${test}\n${failed_tests}")
    fi
done



#---- summary

echo ""
echo "# test output in $OUTPUT_DIR:"
cd $OUTPUT_DIR
ls *.tap

if [[ ${test_errors} -gt 0 ]]; then
    echo ""
    echo "# failing tests:"
    echo "$failed_tests"
    FINAL_EXIT_CODE=1
fi

echo ""
echo "# test results:"

end_time=$(date +%s)
elapsed=$((${end_time} - ${start_time}))

tests=$(awk '/^# tests +[0-9]/ { sum += $(NF) } END { print sum }' $OUTPUT_DIR/*.tap)
passed=$(awk '/^# pass +[0-9]/ { sum += $(NF) } END { print sum }' $OUTPUT_DIR/*.tap)
[[ -z ${tests} ]] && tests=0
[[ -z ${passed} ]] && passed=0
fail=$((${tests} - ${passed}))

echo "# Completed in ${elapsed} seconds."
echo -e "# \033[32mPASS: ${passed} / ${tests}\033[39m"
if [[ ${fail} -gt 0 ]]; then
    echo -e "# \033[31mFAIL: ${fail} / ${tests}\033[39m"
    FINAL_EXIT_CODE=1
elif [[ ${test_errors} -gt 0 ]]; then
    echo -e "# \033[31mFAIL: ? / ${tests}\033[39m"
    FINAL_EXIT_CODE=1
fi
echo ""

exit $FINAL_EXIT_CODE
